/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dp1.pucp.Compras.VIsta;

import com.dp1.pucp.Modelo.Clases.Proveedor;
import com.dp1.pucp.Util.Validaciones;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ubuntu64
 */
public class MantProveedorGUI2 extends javax.swing.JInternalFrame {

    private JFrame padre;

    /**
     * Creates new form MantProveedorGUI2
     */
    public MantProveedorGUI2() {
        initComponents();
        this.setTitle("Mantenimiento de Proveedores");
        try {
            setFrameIcon(new ImageIcon(getClass().getClassLoader().getResource("Imagenes/hombre_icono.png")));
        } catch (Exception ex) {
        }
    }

    public JFrame getPadre() {
        return padre;
    }

    public String getRazonSocial() {
        return txtRazon.getText().trim();
    }
    
    public String getRuc() {
        return txtRUC.getText().trim();
    }

    public int getFilaSel() {
        return tblResultados.getSelectedRow();
    }

    public int getEstado() {
      return cmbEstado.getSelectedIndex()-1;        
    }

    public void addNuevoListener(ActionListener ml) {
        this.btnCrear.addActionListener(ml);
    }

    public void addBuscarListener(ActionListener ml) {
        this.btnBuscar.addActionListener(ml);
    }

    public void addLimpiarListener(ActionListener ml) {
        this.btnLimpiar.addActionListener(ml);
    }

    public void addEditarListener(ActionListener ml) {
        this.btnEditar.addActionListener(ml);
    }

    public void addEliminarListener(ActionListener ml) {
        this.btnEliminar.addActionListener(ml);
    }

    public void addAsignarProductoListener(ActionListener ml) {
        this.btnAsignarProd.addActionListener(ml);
    }

    public void llenaGrilla(ArrayList<Proveedor> lista) {
        if (lista != null) {
            if (lista.size() == 0) {
                mostrarAviso("No se encuentra ningun Proveedor bajo este criterio");
            } else {
                ((DefaultTableModel) this.tblResultados.getModel()).setRowCount(0);
                for (int i = 0; i < lista.size(); i++) {
                    ((DefaultTableModel) tblResultados.getModel()).setRowCount(tblResultados.getRowCount() + 1);
                    tblResultados.setValueAt(lista.get(i).getRazonsocial(), i, 0);
                    tblResultados.setValueAt(lista.get(i).getRuc(), i, 1);
                    tblResultados.setValueAt(lista.get(i).getContacto(), i, 2);
                    tblResultados.setValueAt(lista.get(i).getTelefonocontacto(), i, 3);
                    tblResultados.setValueAt(lista.get(i).getDireccionfiscal(), i, 4);
                    if (lista.get(i).getCategoria() == 1) {
                        tblResultados.setValueAt("Alto", i, 5);
                    }
                    if (lista.get(i).getCategoria() == 2) {
                        tblResultados.setValueAt("Medio", i, 5);
                    }
                    if (lista.get(i).getCategoria() == 3) {
                        tblResultados.setValueAt("Bajo", i, 5);
                    }

                    if (lista.get(i).getEstado() == 1) {
                        tblResultados.setValueAt("Activo", i, 6);
                    }
                    if (lista.get(i).getEstado() == 0) {
                        tblResultados.setValueAt("Inactivo", i, 6);
                    }
//                if(lista.get(i).getEstado() ==3){
//                    tblResultados.setValueAt("Inhabilitado",i,6);
//                }
                }
            }
        }
    }

    public void llenaComboEstado(ArrayList<String> lista) {
        if (lista != null) {
            for (String opc : lista) {
                cmbEstado.addItem(opc);
            }
        }
    }

    public void LimpiarDatos() {
        txtRazon.setText("");
        txtRUC.setText("");
        cmbEstado.setSelectedIndex(0);
        ((DefaultTableModel) this.tblResultados.getModel()).setRowCount(0);
    }

    public void mostrarAviso(String msn) {
        Validaciones.mostrarMensajeAvisoVal(msn);
    }

    public void mostrarError(String msn) {
        Validaciones.mostrarMensajeErrorVal(msn);
    }

    public int solicitarConfirmacion(String msn) {
        return Validaciones.solicitarConfirmacionVal(msn);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtRazon = new javax.swing.JTextField();
        btnLimpiar = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtRUC = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cmbEstado = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblResultados = new javax.swing.JTable();
        btnCrear = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnAsignarProd = new javax.swing.JButton();

        setClosable(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Buscar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 15))); // NOI18N

        jLabel1.setText("Razon Social:");

        txtRazon.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtRazonKeyTyped(evt);
            }
        });

        btnLimpiar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/clear.png"))); // NOI18N
        btnLimpiar.setText("Limpiar");
        btnLimpiar.setBorderPainted(false);
        btnLimpiar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnLimpiar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnLimpiar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        btnBuscar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/buscar.png"))); // NOI18N
        btnBuscar.setText("Buscar");
        btnBuscar.setBorderPainted(false);
        btnBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBuscar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jLabel4.setText("RUC:");

        txtRUC.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtRUCKeyTyped(evt);
            }
        });

        jLabel2.setText("Estado:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(258, 258, 258)
                        .addComponent(btnBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnLimpiar))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(143, 143, 143)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4))
                                .addGap(62, 62, 62)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtRUC, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(txtRazon, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(cmbEstado, 0, 234, Short.MAX_VALUE)))))))
                .addContainerGap(209, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtRazon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtRUC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnBuscar)
                    .addComponent(btnLimpiar))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Resultados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 15))); // NOI18N

        tblResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Razon Social", "RUC", "Contacto", "Teléfono", "Dirección", "Categoria", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblResultados.getTableHeader().setResizingAllowed(false);
        tblResultados.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblResultados);
        tblResultados.getColumnModel().getColumn(0).setMinWidth(150);
        tblResultados.getColumnModel().getColumn(0).setPreferredWidth(150);
        tblResultados.getColumnModel().getColumn(0).setMaxWidth(150);
        tblResultados.getColumnModel().getColumn(1).setMinWidth(100);
        tblResultados.getColumnModel().getColumn(1).setPreferredWidth(100);
        tblResultados.getColumnModel().getColumn(1).setMaxWidth(100);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 676, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                .addContainerGap())
        );

        btnCrear.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnCrear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/nuevo.png"))); // NOI18N
        btnCrear.setText("Agregar");
        btnCrear.setBorderPainted(false);
        btnCrear.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCrear.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnCrear.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        btnEditar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/edit1.png"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.setBorderPainted(false);
        btnEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEditar.setMaximumSize(new java.awt.Dimension(57, 29));
        btnEditar.setMinimumSize(new java.awt.Dimension(57, 29));
        btnEditar.setPreferredSize(new java.awt.Dimension(57, 29));
        btnEditar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        btnEliminar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/delete2.png"))); // NOI18N
        btnEliminar.setText("Inactivar");
        btnEliminar.setBorderPainted(false);
        btnEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEliminar.setMaximumSize(new java.awt.Dimension(57, 29));
        btnEliminar.setMinimumSize(new java.awt.Dimension(57, 29));
        btnEliminar.setPreferredSize(new java.awt.Dimension(57, 29));
        btnEliminar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        btnAsignarProd.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnAsignarProd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/edit2.png"))); // NOI18N
        btnAsignarProd.setText("Productos");
        btnAsignarProd.setBorderPainted(false);
        btnAsignarProd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAsignarProd.setMaximumSize(new java.awt.Dimension(57, 29));
        btnAsignarProd.setMinimumSize(new java.awt.Dimension(57, 29));
        btnAsignarProd.setPreferredSize(new java.awt.Dimension(57, 29));
        btnAsignarProd.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnAsignarProd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAsignarProd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAsignarProdActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnCrear)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAsignarProd, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAsignarProd, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtRazonKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRazonKeyTyped
        // TODO add your handling code here:
        String car = txtRazon.getText();
        if (car.length() >= 70) {
            evt.consume();
        }
        Validaciones.soloDigitoTextoPuntuacion(evt);
    }//GEN-LAST:event_txtRazonKeyTyped

    private void btnAsignarProdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAsignarProdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnAsignarProdActionPerformed

    private void txtRUCKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRUCKeyTyped
        // TODO add your handling code here:
        String car = txtRUC.getText();
        if (car.length() >= 11) {
            evt.consume();
        }
        Validaciones.soloDigito(evt);
    }//GEN-LAST:event_txtRUCKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAsignarProd;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCrear;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cmbEstado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblResultados;
    private javax.swing.JTextField txtRUC;
    private javax.swing.JTextField txtRazon;
    // End of variables declaration//GEN-END:variables
}
